import logging


# 配置日志
def setup_logger(log_file='C:/Users/neiwangji32/ZwAutoTest/log.txt'):
    logger = logging.getLogger('TestLogger')
    logger.setLevel(logging.DEBUG)

    # 创建文件处理器，将日志输出到文件
    file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
    file_handler.setLevel(logging.DEBUG)

    # 创建日志格式
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)

    # 将处理器添加到logger
    logger.addHandler(file_handler)

    return logger


# 使用示例
logger = setup_logger()


def test_function():
    logger.info("Test function started.")
    try:
        # 测试逻辑
        logger.debug("Running test steps...")
        # 假设这里有一个错误
        raise ValueError("Test failed due to some reason!")
    except Exception as e:
        logger.error(f"An error occurred: {e}")
    logger.info("Test function ended.")


# 调用测试函数
test_function()